home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / KOOB / control / server / misc / shapebase.cs < prev    next >
Encoding:
Text File  |  2003-10-26  |  9.9 KB  |  323 lines

  1. //============================================================================
  2. // control/misc/shapebase.cs
  3. //
  4. //
  5. //  Copyright (c) 2003 by Kenneth C.  Finney.
  6. //============================================================================
  7.  
  8. // This file contains ShapeBase methods used by all the derived classes
  9.  
  10. //-----------------------------------------------------------------------------
  11. // ShapeBase object
  12. //-----------------------------------------------------------------------------
  13.  
  14. //-----------------------------------------------------------------------------
  15.  
  16. function ShapeBase::damage(%this, %sourceObject, %position, %damage, %damageType)
  17. {
  18.    // All damage applied by one object to another should go through this
  19.    // method. This function is provided to allow objects some chance of
  20.    // overriding or processing damage values and types.  As opposed to
  21.    // having weapons call ShapeBase::applyDamage directly.
  22.    // Damage is redirected to the datablock, this is standard proceedure
  23.    // for many built in callbacks.
  24.    %this.getDataBlock().damage(%this, %sourceObject, %position, %damage, %damageType);
  25. }
  26.  
  27.  
  28. //-----------------------------------------------------------------------------
  29.  
  30. function ShapeBase::setDamageDt(%this, %damageAmount, %damageType)
  31. {
  32.    // This function is used to apply damage over time.  The damage
  33.    // is applied at a fixed rate (50 ms).  Damage could be applied
  34.    // over time using the built in ShapBase C++ repair functions
  35.    // (using a neg. repair), but this has the advantage of going
  36.    // through the normal script channels.
  37.    if (%obj.getState() !$= "Dead") {
  38.       %this.damage(0, "0 0 0", %damageAmount, %damageType);
  39.       %obj.damageSchedule = %obj.schedule(50, "setDamageDt", %damageAmount, %damageType);
  40.    }
  41.    else
  42.       %obj.damageSchedule = "";
  43. }
  44.  
  45. function ShapeBase::clearDamageDt(%this)
  46. {
  47.    if (%obj.damageSchedule !$= "") {
  48.       cancel(%obj.damageSchedule);
  49.       %obj.damageSchedule = "";
  50.    }
  51. }
  52.  
  53.  
  54. //-----------------------------------------------------------------------------
  55. // ShapeBase datablock
  56. //-----------------------------------------------------------------------------
  57.  
  58. function ShapeBaseData::damage(%this, %obj, %position, %source, %amount, %damageType)
  59. {
  60.    // Ignore damage by default. This empty method is here to
  61.    // avoid console Warnings.
  62. }
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75. // This inventory system is totally scripted, no C++ code involved.
  76. // It uses object datablock names to track inventory and is generally
  77. // object type, or class, agnostic.  In other words, it will inventory
  78. // any kind of ShapeBase object, though the throw method does assume
  79. // that the objects are small enough to throw :)
  80. //
  81. // For a ShapeBase object to support inventory, it must have an array
  82. // of inventory max values:
  83. //
  84. //    %this.maxInv[GunAmmo] = 100;
  85. //    %this.maxInv[SpeedGun] = 1;
  86. //
  87. // where the names "SpeedGun" and "GunAmmo" are datablocks.
  88. //
  89. // For objects to be inventoriable, they must provide a set of inventory
  90. // callback methods, mainly:
  91. //
  92. //    onUse
  93. //    onThrow
  94. //    onPickup
  95. //
  96. // Example methods are given further down.  The item.cs file also contains
  97. // example inventory items.
  98.  
  99. //-----------------------------------------------------------------------------
  100.  
  101.  
  102. //-----------------------------------------------------------------------------
  103. // ShapeBase inventory support
  104. //-----------------------------------------------------------------------------
  105.  
  106. //-----------------------------------------------------------------------------
  107.  
  108. function ShapeBase::use(%this,%data)
  109. {
  110.    // Use an object in the inventory.
  111.    if (%this.getInventory(%data) > 0)
  112.       return %data.onUse(%this);
  113.    return false;
  114. }
  115.  
  116. function ShapeBase::throw(%this,%data,%amount)
  117. {
  118.    // Throw objects from inventory. The onThrow method is
  119.    // responsible for decrementing the inventory.
  120.    if (%this.getInventory(%data) > 0) {
  121.       %obj = %data.onThrow(%this,%amount);
  122.       if (%obj) {
  123.          %this.throwObject(%obj);
  124.          return true;
  125.       }
  126.    }
  127.    return false;
  128. }
  129.  
  130. function ShapeBase::pickup(%this,%obj,%amount)
  131. {
  132.    // This method is called to pickup an object and add it
  133.    // to the inventory. The datablock onPickup method is actually
  134.    // responsible for doing all the work, including incrementing
  135.    // the inventory.
  136.    %data = %obj.getDatablock();
  137.  
  138.    // Try and pickup the max if no value was specified
  139.    if (%amount $= "")
  140.       %amount = %this.maxInventory(%data) - %this.getInventory(%data);
  141.  
  142.    // The datablock does the work...
  143.    if (%amount < 0)
  144.       %amount = 0;
  145.    if (%amount)
  146.       return %data.onPickup(%obj,%this,%amount);
  147.    return false;
  148. }
  149.  
  150. //-----------------------------------------------------------------------------
  151.  
  152. function ShapeBase::maxInventory(%this,%data)
  153. {
  154.    // If there is no limit defined, we assume 0
  155.    return %this.getDatablock().maxInv[%data.getName()];
  156. }
  157.  
  158. function ShapeBase::incInventory(%this,%data,%amount)
  159. {
  160.    // Increment the inventory by the given amount.  The return value
  161.    // is the amount actually added, which may be less than the
  162.    // requested amount due to inventory restrictions.
  163.    %max = %this.maxInventory(%data);
  164.    %total = %this.inv[%data.getName()];
  165.    if ( (%data.getName()$="Gold") || (%data.getName()$="Copper") || (%data.getName()$="Silver") )
  166.    {
  167.      %val = Scorebox.GetValue() + %data.value;
  168.      Scorebox.SetValue(%val);
  169.    }
  170.  
  171.    if (%total < %max) {
  172.       if (%total + %amount > %max)
  173.          %amount = %max - %total;
  174.       %this.setInventory(%data,%total + %amount);
  175.       return %amount;
  176.    }
  177.    return 0;
  178. }
  179.  
  180. function ShapeBase::decInventory(%this,%data,%amount)
  181. {
  182.    // Decrement the inventory by the given amount. The return value
  183.    // is the amount actually removed.
  184.    %total = %this.inv[%data.getName()];
  185.    if (%total > 0) {
  186.       if (%total < %amount)
  187.          %amount = %total;
  188.       %this.setInventory(%data,%total - %amount);
  189.       return %amount;
  190.    }
  191.    return 0;
  192. }
  193.  
  194.  
  195. //-----------------------------------------------------------------------------
  196.  
  197. function ShapeBase::getInventory(%this,%data)
  198. {
  199.    // Return the current inventory amount
  200.    return %this.inv[%data.getName()];
  201. }
  202.  
  203. function ShapeBase::setInventory(%this,%data,%value)
  204. {
  205.    // Set the inventory amount for this datablock and invoke
  206.    // inventory callbacks.  All changes to inventory go through this
  207.    // single method.
  208.  
  209.    // Impose inventory limits
  210.    if (%value < 0)
  211.       %value = 0;
  212.    else {
  213.       %max = %this.maxInventory(%data);
  214.       if (%value > %max)
  215.          %value = %max;
  216.    }
  217.  
  218.    // Set the value and invoke object callbacks
  219.    %name = %data.getName();
  220.    if (%this.inv[%name] != %value)
  221.    {
  222.       %this.inv[%name] = %value;
  223.       %data.onInventory(%this,%value);
  224.       %this.getDataBlock().onInventory(%data,%value);
  225.    }
  226.    return %value;
  227. }
  228.  
  229.  
  230. //-----------------------------------------------------------------------------
  231.  
  232. function ShapeBase::clearInventory(%this)
  233. {
  234.    // To be filled in...
  235. }
  236.  
  237.  
  238. //-----------------------------------------------------------------------------
  239.  
  240. function ShapeBase::throwObject(%this,%obj)
  241. {
  242.    // Throw the given object in the direction the shape is looking.
  243.    // The force value is hardcoded according to the current default
  244.    // object mass and mission gravity (20m/s^2).
  245.    %throwForce = %this.throwForce;
  246.    if (!%throwForce)
  247.       %throwForce = 20;
  248.  
  249.    // Start with the shape's eye vector...
  250.    %eye = %this.getEyeVector();
  251.    %vec = vectorScale(%eye, %throwForce);
  252.  
  253.    // Add a vertical component to give the object a better arc
  254.    %verticalForce = %throwForce / 2;
  255.    %dot = vectorDot("0 0 1",%eye);
  256.    if (%dot < 0)
  257.       %dot = -%dot;
  258.    %vec = vectorAdd(%vec,vectorScale("0 0 " @ %verticalForce,1 - %dot));
  259.  
  260.    // Add the shape's velocity
  261.    %vec = vectorAdd(%vec,%this.getVelocity());
  262.  
  263.    // Set the object's position and initial velocity
  264.    %pos = getBoxCenter(%this.getWorldBox());
  265.    %obj.setTransform(%pos);
  266.    %obj.applyImpulse(%pos,%vec);
  267.  
  268.    // Since the object is thrown from the center of the
  269.    // shape, the object needs to avoid colliding with it's
  270.    // thrower.
  271.    %obj.setCollisionTimeout(%this);
  272. }
  273.  
  274.  
  275. //-----------------------------------------------------------------------------
  276. // Callback hooks invoked by the inventory system
  277. //-----------------------------------------------------------------------------
  278.  
  279. //-----------------------------------------------------------------------------
  280. // ShapeBase object callbacks invoked by the inventory system
  281.  
  282. function ShapeBase::onInventory(%this, %data, %value)
  283. {
  284.    // Invoked on ShapeBase objects whenever their inventory changes
  285.    // for the given datablock.
  286. }
  287.  
  288.  
  289. //-----------------------------------------------------------------------------
  290. // ShapeBase datablock callback invoked by the inventory system.
  291.  
  292. function ShapeBaseData::onUse(%this,%user)
  293. {
  294.    // Invoked when the object uses this datablock, should return
  295.    // true if the item was used.
  296.    return false;
  297. }
  298.  
  299. function ShapeBaseData::onThrow(%this,%user,%amount)
  300. {
  301.    // Invoked when the object is thrown.  This method should
  302.    // construct and return the actual mission object to be
  303.    // physically thrown.  This method is also responsible for
  304.    // decrementing the user's inventory.
  305.    return 0;
  306. }
  307.  
  308. function ShapeBaseData::onPickup(%this,%obj,%user,%amount)
  309. {
  310.    // Invoked when the user attempts to pickup this datablock object.
  311.    // The %amount argument is the space in the user's inventory for
  312.    // this type of datablock.  This method is responsible for
  313.    // incrementing the user's inventory is something is addded.
  314.    // Should return true if something was added to the inventory.
  315.    return false;
  316. }
  317.  
  318. function ShapeBaseData::onInventory(%this,%user,%value)
  319. {
  320.    // Invoked whenever an user's inventory total changes for
  321.    // this datablock.
  322. }
  323.